package com.example.month11.festivalsms.fragment;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

import androidx.fragment.app.ListFragment;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;

import com.example.month11.festivalsms.bean.SendedMsg;
import com.example.month11.festivalsms.db.SmsProvider;
import com.example.month11.flowlayout.view.FlowLayout;
import com.example.recyclerview01.R;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * 发送记录.
 * ListFragment中ListView的使用.
 * CursorAdapter的使用.动态更新数据，实时展示数据 (Loader+LoaderManager)
 */
public class SmsHistoryFragment extends ListFragment {
    public static final int LOADER_ID = 1;
    private LayoutInflater mInflater;
    private CursorAdapter mCursorAdapter;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mInflater = LayoutInflater.from(getContext());

        initLoader();
        setupListAdapter();
    }

    private void setupListAdapter() {
        mCursorAdapter = new CursorAdapter(getActivity(), null, false) {
            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
                return mInflater.inflate(R.layout.item_sended_msg, parent, false);
            }

            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                // 对控件赋值
                TextView tvMsg = view.findViewById(R.id.id_tv_msg);
                FlowLayout flContacts = view.findViewById(R.id.id_fl_contacts);
                TextView tvFestival = view.findViewById(R.id.id_tv_festival);
                TextView tvDate = view.findViewById(R.id.id_tv_date);

                tvMsg.setText(cursor.getString(cursor.getColumnIndex(SendedMsg.COLUMN_MSG)));
                tvFestival.setText(cursor.getString(cursor.getColumnIndex(SendedMsg.COLUMN_FESTIVAL_NAME)));
                long dataL = cursor.getLong(cursor.getColumnIndex(SendedMsg.COLUMN_DATE));
                tvDate.setText(parseDate(dataL));

                String names = cursor.getString(cursor.getColumnIndex(SendedMsg.COLUMN_NAMES));
                if (TextUtils.isEmpty(names)) return;
                flContacts.removeAllViews(); // 清空,避免控件服用,导致的bug.
                for (String name : names.split(":")) { // 接收人,名称显示.
                    addTag(name, flContacts);
                }
            }
        };
        setListAdapter(mCursorAdapter);
    }

    private String parseDate(long dataL) {
        DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        return df.format(dataL);
    }

    private void addTag(String name, FlowLayout fl) {
        TextView tv = (TextView) mInflater.inflate(R.layout.tag, fl, false);
        tv.setText(name);
        fl.addView(tv);
    }

    // 初始化Loader
    private void initLoader() {
        getLoaderManager().initLoader(LOADER_ID, null, new LoaderManager.LoaderCallbacks<Cursor>() {
            @Override
            public Loader<Cursor> onCreateLoader(int id, Bundle args) {
                CursorLoader loader = new CursorLoader(getActivity(), SmsProvider.URI_SMS_ALL,
                        null, null, null, null);
                return loader;
            }

            @Override
            public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
                if (loader.getId() == LOADER_ID) {
                    mCursorAdapter.swapCursor(data);
                }
            }

            @Override
            public void onLoaderReset(Loader<Cursor> loader) {
                mCursorAdapter.swapCursor(null);
            }
        });
    }

}
